#include "Drivers/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h" // Device header
#include "stm32f4xx_conf.h"
#include "Bluetooth.h"



// 接收缓冲区
static char rx_buf[RX_BUF_SIZE];
static volatile uint8_t rx_head = 0, rx_tail = 0;

void USART3_Init(void) {
    GPIO_InitTypeDef GPIO_InitStruct;
    USART_InitTypeDef USART_InitStruct;
    NVIC_InitTypeDef NVIC_InitStruct;
    
    // 1. 使能时钟
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
    
    // 2. 配置PC6(TX)和PC7(RX)
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOB, &GPIO_InitStruct);
    
    // 3. 复用功能
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
    
    // 4. 配置USART3
    USART_InitStruct.USART_BaudRate = USART3_BAUDRATE;
    USART_InitStruct.USART_WordLength = USART_WordLength_8b;
    USART_InitStruct.USART_StopBits = USART_StopBits_1;
    USART_InitStruct.USART_Parity = USART_Parity_No;
    USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_Init(USART3, &USART_InitStruct);
    
    // 5. 配置中断
    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
    NVIC_InitStruct.NVIC_IRQChannel = USART3_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 3;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 3;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);
    
    // 6. 使能USART
    USART_Cmd(USART3, ENABLE);
}

void USART3_SendChar(char c) {
    while (!(USART3->SR & USART_FLAG_TXE));
    USART3->DR = c;
}

void USART3_SendString(const char *str) {
    while (*str) USART3_SendChar(*str++);
}

// 读取一个字符(非阻塞)
// 返回1表示成功读取，0表示无数据
uint8_t USART3_ReadChar(char *c) {
    if (rx_head == rx_tail) return 0;
    
    *c = rx_buf[rx_tail];
    rx_tail = (rx_tail + 1) % RX_BUF_SIZE;
    return 1;
}

// 中断服务函数
void USART3_IRQHandler(void) {
    if (USART_GetITStatus(USART3, USART_IT_RXNE)) {
        // 读取数据
        char data = USART_ReceiveData(USART3);
        
        // 存入缓冲区
        uint8_t next_head = (rx_head + 1) % RX_BUF_SIZE;
        if (next_head != rx_tail) {
            rx_buf[rx_head] = data;
            rx_head = next_head;
        }
        
        USART_ClearITPendingBit(USART3, USART_IT_RXNE);
    }
}

//使用示例（与2.4G模块一致）
//USART3_Init();
//USART3_SendString("USART3 Ready\r\n");
//		char c;
//     if (USART3_ReadChar(&c)) {
//            // 回显接收到的字符
//         USART3_SendChar(c);
//         OLED_ShowChar(1, 6, c);
//				if(c=='a')
//				{
//					LED_ON(1);
//				}
//				if(c=='b')
//				{
//					LED_OFF(1);
//				}
//            // 特殊处理回车
//         if (c == '\r') USART3_SendChar('\n');
//     }
